from fastapi import FastAPI
from pydantic import BaseModel
from clock_setting import ClockSettingsDto, ClockSettings
from clock import Clock
# docs http://ip:port/docs
class Time(BaseModel):
    hour:int
    minute:int

app=FastAPI()

clock=Clock(ClockSettings.load_from_file())

@app.get('/clock/settings')
def get_settings():
    return clock.settings.to_dto()

@app.get('/clock/test')
def test_clock():
    clock.alarm_test()

@app.get('/clock/next')
def get_next_time():
    h,m=clock.next_alarm_time()
    return Time(hour=h,minute=m)

@app.post('/clock/settings')
def update_settings(settings:ClockSettingsDto):
    clock.update_settings(ClockSettings.load_from_dto(settings))
    return clock.settings.to_dto()

if __name__ == '__main__':
    import uvicorn
    clock.run_async()
    uvicorn.run(app,host='0.0.0.0',port=8099)